package t327.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import t327.pojo.Client;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.until.Result;
import t327.service.iter.IClientService;

import java.util.List;

@Controller
@RequestMapping("ClientS")
public class ClientController {
    @Autowired
    private IClientService iClientService;

    @RequestMapping("addCli")
    @ResponseBody
    public Result addCli(@RequestBody Client client){
        int add =iClientService.addClient(client);
        if (add > 0) {
            return Result.success();
        } else {
            return new Result(202, "新增失败", false, null);
        }

    }

    @RequestMapping("deleteClientById")
    @ResponseBody
    public Result deleteClientById(Long clientId) {
        System.out.println(clientId + "前端参数");
        int i = iClientService.deleteClientById(clientId);
        System.out.println(i + "修改结果");
        if (i > 0) {
            return Result.success();
        } else {
            return new Result(202, "删除失败", false, null);
        }

    }

    @RequestMapping("getClientInfo")
    @ResponseBody
    private LuiTableEntity getClientInfo(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit") Integer pageSize) {
        List<Client> clients1 = iClientService.queryAll();
        LuiTableEntity luiTableEntity = new LuiTableEntity<Client>();
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Client> menuPageInfo = new PageInfo<>(clients1);
        List<Client> clients = menuPageInfo.getList();
        long total = menuPageInfo.getTotal();
        if (clients != null && clients.size() > 0) {
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(clients);
        } else {
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("查询失败");
        }
        return luiTableEntity;

    }
    @RequestMapping("updateClient")
    @ResponseBody
    public String updateMenu(@RequestBody Client client){
        return iClientService.updateClient(client)>0?"success":"fail";
    }
    //渲染主页数据
    @RequestMapping("clientHomePageRendering")
    @ResponseBody
    public List<Client> clientHomePageRendering(){
        return iClientService.queryAll();
    }
}



